package com.lc.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.yygh.cmnclient.DictFeignClient;
import com.lc.yygh.enums.DictEnum;
import com.lc.yygh.model.user.Patient;
import com.lc.yygh.user.mapper.PatientMapper;
import com.lc.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;//远程调用的中间接口
    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient>wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patients = baseMapper.selectList(wrapper);
        //对patients做包装，显示我们想要的内容
        for (Patient patient : patients) {
            this.packPatient(patient);//将每个就诊人进行包装
        }
        return patients;
    }

    @Override
    public Patient getPatientById(Long id) {
        Patient patient = this.packPatient(this.getById(id));
        return patient;
    }

    //Patient对象里面其它参数封装
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取就诊人证件类型
        String certificatesTypeString  = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //省
        String provinceString  = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        //获取联系人的证件类型名称
        String contactsCertificatesTypeString   = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        /*上边五个值是要在页面上显示的*/
        //封装起来
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
